package com.aiju.cash.ui.good.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.bean.Good;
import com.aiju.bean.GoodsType;
import com.aiju.cash.R;
import com.aiju.widgit.ShoppingView;
import com.bumptech.glide.Glide;

/**
 * @author: 佐为
 * @date: 2017-01-09 17:16
 * @version: V1.0
 * @Description: 描述
 */
public class GoodManagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //上下文Context
    private Context context;

    private GoodsType goodsType;

    private boolean delete;

    public void setDelete(boolean delete) {
        this.delete = delete;
        if (!delete) {
            for (Good good : goodsType.getGoodsList()) {
                good.setSelect(false);
            }
        }
        notifyDataSetChanged();
    }

    /**
     * 构造器
     *
     * @param context
     * @param goodsType
     */
    public GoodManagerAdapter(Context context, GoodsType goodsType) {
        this.context = context;
        this.goodsType = goodsType;
    }

    @Override
    public int getItemCount() {
        return goodsType.getGoodsList().size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_team, parent, false);

        return new ContentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;

        final Good good = goodsType.getGoodsList().get(position);

        viewHolder.nameTextView.setText(good.getName());
        if (good.isWeighGood()) {
            viewHolder.priceTextView.setText(good.getPrice() + "/" + good.getUnit());
        } else {
            viewHolder.priceTextView.setText(good.getPrice());
        }
        viewHolder.rootView.setSelected(good.isSelect());
        viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position);
                }
                if (delete) {
                    v.setSelected(!v.isSelected());
                    if (goodsType.getSelectGoods().contains(good)) {
                        goodsType.getSelectGoods().remove(good);
                        good.setSelect(false);
                    } else {
                        goodsType.getSelectGoods().add(good);
                        good.setSelect(true);
                    }
                }
            }
        });

        if (delete) {
            viewHolder.imageViewDelete.setVisibility(View.VISIBLE);
            viewHolder.shoppingView.setVisibility(View.GONE);
        } else {
            viewHolder.imageViewDelete.setVisibility(View.GONE);
            viewHolder.shoppingView.setVisibility(View.GONE);
        }

        Glide.with(context)
                .load(good.getImageUrl())
                .placeholder(R.mipmap.icon_empty)
                .centerCrop()
                .into(viewHolder.imageViewTeam);
    }


    private class ContentViewHolder extends RecyclerView.ViewHolder {
        View rootView;
        TextView nameTextView;
        TextView priceTextView;
        ImageView imageViewTeam;
        ImageView imageViewDelete;
        ShoppingView shoppingView;

        public ContentViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            nameTextView = (TextView) itemView.findViewById(R.id.tv_good_name);
            priceTextView = (TextView) itemView.findViewById(R.id.tv_good_price);
            imageViewTeam = (ImageView) itemView.findViewById(R.id.iv_good_pic);
            imageViewDelete = (ImageView) itemView.findViewById(R.id.iv_good_delete);
            shoppingView = (ShoppingView) itemView.findViewById(R.id.shop_num);
        }
    }

    //点击事件
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

}
